package cn.gcBlock.utils;

import java.io.FileWriter;
import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.IContext;


public class TemplateUtils {

    @Autowired
    public static final TemplateEngine engine = SpringContextUtils.getBean(TemplateEngine.class);
    /**
     * 生成静态文件
     * @param freeTempName 模板名称
     * @param context 数据内容
     * @param outFilePath 输出路径
     * @return
     */
    public static boolean process(String freeTempName, IContext context, String outFilePath) {
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(outFilePath);
            engine.process(freeTempName, context,fileWriter);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }

    public static String process(String freeTempName, IContext context) {
            String html = engine.process(freeTempName, context);
            return html;
    }
}
